package fairygui.editor.gui
{
   public class ERelationItem
   {
      
      private static const RELATION_NAMES:Array = ["left-left","left-center","left-right","center-center","right-left","right-center","right-right","top-top","top-middle","top-bottom","middle-middle","bottom-top","bottom-middle","bottom-bottom","width-width","height-height","leftext-left","leftext-right","rightext-left","rightext-right","topext-top","topext-bottom","bottomext-top","bottomext-bottom"];
       
      
      private var _owner:EGObject;
      
      private var _target:EGObject;
      
      private var _defs:Vector.<ERelationDef>;
      
      private var _targetX:Number;
      
      private var _targetY:Number;
      
      private var _targetWidth:Number;
      
      private var _targetHeight:Number;
      
      private var _readOnly:Boolean;
      
      public function ERelationItem(param1:EGObject)
      {
         super();
         this._owner = param1;
         this._defs = new Vector.<ERelationDef>(2);
         this._defs[0] = new ERelationDef();
         this._defs[1] = new ERelationDef();
      }
      
      public function get owner() : EGObject
      {
         return this._owner;
      }
      
      public function get readOnly() : Boolean
      {
         return this._readOnly;
      }
      
      public function set(param1:EGObject, param2:String, param3:Boolean = false) : void
      {
         this._readOnly = param3;
         this.target = param1;
         this.updateDefs(param2);
      }
      
      public function updateDefs(param1:String) : void
      {
         var _loc3_:String = null;
         var _loc4_:String = null;
         var _loc5_:String = null;
         var _loc6_:Boolean = false;
         var _loc7_:int = 0;
         var _loc8_:int = 0;
         var _loc9_:ERelationDef = null;
         var _loc11_:int = 0;
         var _loc2_:Array = param1.split(",");
         _loc7_ = 0;
         while(_loc7_ < 2)
         {
            this._defs[_loc7_].valid = false;
            _loc7_++;
         }
         var _loc10_:int = 0;
         _loc7_ = 0;
         while(_loc7_ < 2)
         {
            _loc3_ = _loc2_[_loc7_];
            if(_loc3_)
            {
               _loc9_ = this._defs[_loc10_++];
               if(_loc3_.charAt(_loc3_.length - 1) == "%")
               {
                  _loc3_ = _loc3_.substr(0,_loc3_.length - 1);
                  _loc6_ = true;
               }
               else
               {
                  _loc6_ = false;
               }
               _loc11_ = _loc3_.indexOf("-");
               if(_loc11_ == -1)
               {
                  _loc4_ = _loc3_;
                  _loc5_ = _loc3_;
               }
               else
               {
                  _loc4_ = _loc3_.substring(0,_loc11_);
                  _loc5_ = _loc3_.substring(_loc11_ + 1);
               }
               _loc8_ = RELATION_NAMES.indexOf(_loc4_ + "-" + _loc5_);
               if(_loc8_ == -1)
               {
                  throw new Error("invalid relation type");
               }
               _loc9_.selfSide = _loc4_;
               _loc9_.targetSide = _loc5_;
               _loc9_.affectBySelfSizeChanged = _loc8_ >= 3 && _loc8_ <= 6 || _loc8_ >= 10 && _loc8_ <= 13;
               _loc9_.percent = _loc6_;
               _loc9_.type = _loc8_;
               _loc9_.valid = true;
            }
            _loc7_++;
         }
      }
      
      public function get target() : EGObject
      {
         return this._target;
      }
      
      public function set target(param1:EGObject) : void
      {
         if(this._target != param1)
         {
            if(this._target)
            {
               this.releaseRefTarget(this._target);
            }
            this._target = param1;
            if(this._target)
            {
               this.addRefTarget(this._target);
            }
         }
      }
      
      public function get defs() : Vector.<ERelationDef>
      {
         return this._defs;
      }
      
      public function get containsWidthRelated() : Boolean
      {
         return this._defs[0].valid && (this._defs[0].type == ERelationType.Width || this._defs[0].type == ERelationType.RightExt_Right) || this._defs[1].valid && (this._defs[1].type == ERelationType.Width || this._defs[1].type == ERelationType.RightExt_Right);
      }
      
      public function get containsHeightRelated() : Boolean
      {
         return this._defs[0].valid && (this._defs[0].type == ERelationType.Height || this._defs[0].type == ERelationType.BottomExt_Bottom) || this._defs[1].valid && (this._defs[1].type == ERelationType.Height || this._defs[1].type == ERelationType.BottomExt_Bottom);
      }
      
      public function dispose() : void
      {
         if(this._target)
         {
            this.releaseRefTarget(this._target);
            this._target = null;
         }
      }
      
      public function get sidePair() : String
      {
         var _loc2_:ERelationDef = null;
         var _loc1_:* = "";
         _loc2_ = this._defs[0];
         if(_loc2_.valid)
         {
            _loc1_ = _loc2_.selfSide + "-" + _loc2_.targetSide + (!!_loc2_.percent?"%":"");
         }
         _loc2_ = this._defs[1];
         if(_loc2_.valid)
         {
            if(_loc1_.length)
            {
               _loc1_ = _loc1_ + ",";
            }
            _loc1_ = _loc1_ + (_loc2_.selfSide + "-" + _loc2_.targetSide + (!!_loc2_.percent?"%":""));
         }
         return _loc1_;
      }
      
      private function applyXYChanged(param1:Number, param2:Number) : void
      {
         var _loc3_:ERelationDef = null;
         if((this._owner.editMode == 3 && EGComponent(this._owner).disabled_relations || this._owner.parent && this._owner.parent.disabled_relations) && (this._target.editMode == 2 || this._target.editMode == 3))
         {
            return;
         }
         _loc3_ = this._defs[0];
         if(_loc3_.valid)
         {
            this.applyXYChanged2(_loc3_,param1,param2);
         }
         _loc3_ = this._defs[1];
         if(_loc3_.valid)
         {
            this.applyXYChanged2(_loc3_,param1,param2);
         }
      }
      
      private function applySizeChanged() : void
      {
         var _loc1_:ERelationDef = null;
         if((this._owner.editMode == 3 && EGComponent(this._owner).disabled_relations || this._owner.parent && this._owner.parent.disabled_relations) && (this._target.editMode == 2 || this._target.editMode == 3))
         {
            return;
         }
         _loc1_ = this._defs[0];
         if(_loc1_.valid)
         {
            this.applySizeChanged2(_loc1_);
         }
         _loc1_ = this._defs[1];
         if(_loc1_.valid)
         {
            this.applySizeChanged2(_loc1_);
         }
      }
      
      public function applySelfSizeChanged(param1:Number, param2:Number) : void
      {
         var _loc3_:ERelationDef = null;
         if(this._owner.editMode == 3 && EGComponent(this._owner).disabled_relations == 2 || this._owner.parent && this._owner.parent.disabled_relations == 2)
         {
            return;
         }
         _loc3_ = this._defs[0];
         if(_loc3_.valid && _loc3_.affectBySelfSizeChanged)
         {
            this.applySelfSizeChanged2(_loc3_,param1,param2);
         }
         _loc3_ = this._defs[1];
         if(_loc3_.valid && _loc3_.affectBySelfSizeChanged)
         {
            this.applySelfSizeChanged2(_loc3_,param1,param2);
         }
      }
      
      private function applyXYChanged2(param1:ERelationDef, param2:Number, param3:Number) : void
      {
         switch(param1.type)
         {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
               this._owner.x = this._owner.x + param2;
               break;
            case 7:
            case 8:
            case 9:
            case 10:
            case 11:
            case 12:
            case 13:
               this._owner.y = this._owner.y + param3;
               break;
            case 14:
            case 15:
               break;
            case 16:
            case 17:
               this._owner.x = this._owner.x + param2;
               this._owner.width = this._owner._rawWidth - param2;
               break;
            case 18:
            case 19:
               this._owner.width = this._owner._rawWidth + param2;
               break;
            case 20:
            case 21:
               this._owner.y = this._owner.y + param3;
               this._owner.height = this._owner._rawHeight - param3;
               break;
            case 22:
            case 23:
               this._owner.height = this._owner._rawHeight + param3;
         }
      }
      
      private function applySizeChanged2(param1:ERelationDef) : void
      {
         var _loc2_:Number = NaN;
         var _loc3_:Number = NaN;
         var _loc4_:Number = NaN;
         var _loc5_:Number = NaN;
         if(this._target != this._owner.parent)
         {
            _loc2_ = this._target.x;
            _loc3_ = this._target.y;
         }
         else
         {
            _loc2_ = 0;
            _loc3_ = 0;
         }
         switch(param1.type)
         {
            case ERelationType.Left_Left:
               if(param1.percent && this._target == this._owner.parent)
               {
                  _loc4_ = this._owner.x - _loc2_;
                  if(param1.percent)
                  {
                     _loc4_ = _loc4_ / this._targetWidth * this._target._width;
                  }
                  this._owner.x = _loc2_ + _loc4_;
               }
               break;
            case ERelationType.Left_Center:
               _loc4_ = this._owner.x - (_loc2_ + this._targetWidth / 2);
               if(param1.percent)
               {
                  _loc4_ = _loc4_ / this._targetWidth * this._target._width;
               }
               this._owner.x = _loc2_ + this._target._width / 2 + _loc4_;
               break;
            case ERelationType.Left_Right:
               _loc4_ = this._owner.x - (_loc2_ + this._targetWidth);
               if(param1.percent)
               {
                  _loc4_ = _loc4_ / this._targetWidth * this._target._width;
               }
               this._owner.x = _loc2_ + this._target._width + _loc4_;
               break;
            case ERelationType.Center_Center:
               _loc4_ = this._owner.x + this._owner._rawWidth / 2 - (_loc2_ + this._targetWidth / 2);
               if(param1.percent)
               {
                  _loc4_ = _loc4_ / this._targetWidth * this._target._width;
               }
               this._owner.x = _loc2_ + this._target._width / 2 + _loc4_ - this._owner._rawWidth / 2;
               break;
            case ERelationType.Right_Left:
               _loc4_ = this._owner.x + this._owner._rawWidth - _loc2_;
               if(param1.percent)
               {
                  _loc4_ = _loc4_ / this._targetWidth * this._target._width;
               }
               this._owner.x = _loc2_ + _loc4_ - this._owner._rawWidth;
               break;
            case ERelationType.Right_Center:
               _loc4_ = this._owner.x + this._owner._rawWidth - (_loc2_ + this._targetWidth / 2);
               if(param1.percent)
               {
                  _loc4_ = _loc4_ / this._targetWidth * this._target._width;
               }
               this._owner.x = _loc2_ + this._target._width / 2 + _loc4_ - this._owner._rawWidth;
               break;
            case ERelationType.Right_Right:
               _loc4_ = this._owner.x + this._owner._rawWidth - (_loc2_ + this._targetWidth);
               if(param1.percent)
               {
                  _loc4_ = _loc4_ / this._targetWidth * this._target._width;
               }
               this._owner.x = _loc2_ + this._target._width + _loc4_ - this._owner._rawWidth;
               break;
            case ERelationType.Top_Top:
               if(param1.percent && this._target == this._owner.parent)
               {
                  _loc4_ = this._owner.y - _loc3_;
                  if(param1.percent)
                  {
                     _loc4_ = _loc4_ / this._targetHeight * this._target._height;
                  }
                  this._owner.y = _loc3_ + _loc4_;
               }
               break;
            case ERelationType.Top_Middle:
               _loc4_ = this._owner.y - (_loc3_ + this._targetHeight / 2);
               if(param1.percent)
               {
                  _loc4_ = _loc4_ / this._targetHeight * this._target._height;
               }
               this._owner.y = _loc3_ + this._target._height / 2 + _loc4_;
               break;
            case ERelationType.Top_Bottom:
               _loc4_ = this._owner.y - (_loc3_ + this._targetHeight);
               if(param1.percent)
               {
                  _loc4_ = _loc4_ / this._targetHeight * this._target._height;
               }
               this._owner.y = _loc3_ + this._target._height + _loc4_;
               break;
            case ERelationType.Middle_Middle:
               _loc4_ = this._owner.y + this._owner._rawHeight / 2 - (_loc3_ + this._targetHeight / 2);
               if(param1.percent)
               {
                  _loc4_ = _loc4_ / this._targetHeight * this._target._height;
               }
               this._owner.y = _loc3_ + this._target._height / 2 + _loc4_ - this._owner._rawHeight / 2;
               break;
            case ERelationType.Bottom_Top:
               _loc4_ = this._owner.y + this._owner._rawHeight - _loc3_;
               if(param1.percent)
               {
                  _loc4_ = _loc4_ / this._targetHeight * this._target._height;
               }
               this._owner.y = _loc3_ + _loc4_ - this._owner._rawHeight;
               break;
            case ERelationType.Bottom_Middle:
               _loc4_ = this._owner.y + this._owner._rawHeight - (_loc3_ + this._targetHeight / 2);
               if(param1.percent)
               {
                  _loc4_ = _loc4_ / this._targetHeight * this._target._height;
               }
               this._owner.y = _loc3_ + this._target._height / 2 + _loc4_ - this._owner._rawHeight;
               break;
            case ERelationType.Bottom_Bottom:
               _loc4_ = this._owner.y + this._owner._rawHeight - (_loc3_ + this._targetHeight);
               if(param1.percent)
               {
                  _loc4_ = _loc4_ / this._targetHeight * this._target._height;
               }
               this._owner.y = _loc3_ + this._target._height + _loc4_ - this._owner._rawHeight;
               break;
            case ERelationType.Width:
               if(this._owner.editMode != 3 && this._owner == this._target.parent)
               {
                  _loc4_ = this._owner.sourceWidth - this._target._initWidth;
               }
               else
               {
                  _loc4_ = this._owner._rawWidth - this._targetWidth;
               }
               if(param1.percent)
               {
                  _loc4_ = _loc4_ / this._targetWidth * this._target._width;
               }
               if(this._target == this._owner.parent)
               {
                  this._owner.setSize(this._target._width + _loc4_,this._owner._rawHeight,true);
               }
               else
               {
                  this._owner.width = this._target._width + _loc4_;
               }
               break;
            case ERelationType.Height:
               if(this._owner.editMode != 3 && this._owner == this._target.parent)
               {
                  _loc4_ = this._owner.sourceHeight - this._target._initHeight;
               }
               else
               {
                  _loc4_ = this._owner._rawHeight - this._targetHeight;
               }
               if(param1.percent)
               {
                  _loc4_ = _loc4_ / this._targetHeight * this._target._height;
               }
               if(this._target == this._owner.parent)
               {
                  this._owner.setSize(this._owner._rawWidth,this._target._height + _loc4_,true);
               }
               else
               {
                  this._owner.height = this._target._height + _loc4_;
               }
               break;
            case ERelationType.LeftExt_Left:
               break;
            case ERelationType.LeftExt_Right:
               _loc4_ = this._owner.x - (_loc2_ + this._targetWidth);
               if(param1.percent)
               {
                  _loc4_ = _loc4_ / this._targetWidth * this._target._width;
               }
               _loc5_ = this._owner.x;
               this._owner.x = _loc2_ + this._target._width + _loc4_;
               this._owner.width = this._owner._rawWidth - (this._owner.x - _loc5_);
               break;
            case ERelationType.RightExt_Left:
               break;
            case ERelationType.RightExt_Right:
               if(this._owner.editMode != 3 && this._owner == this._target.parent)
               {
                  _loc4_ = this._owner.sourceWidth - (_loc2_ + this._target._initWidth);
               }
               else
               {
                  _loc4_ = this._owner._rawWidth - (_loc2_ + this._targetWidth);
               }
               if(this._owner != this._target.parent)
               {
                  _loc4_ = _loc4_ + this._owner.x;
               }
               if(param1.percent)
               {
                  _loc4_ = _loc4_ / this._targetWidth * this._target._width;
               }
               if(this._owner != this._target.parent)
               {
                  this._owner.width = _loc2_ + this._target._width + _loc4_ - this._owner.x;
               }
               else
               {
                  this._owner.width = _loc2_ + this._target._width + _loc4_;
               }
               break;
            case ERelationType.TopExt_Top:
               break;
            case ERelationType.TopExt_Bottom:
               _loc4_ = this._owner.y - (_loc3_ + this._targetHeight);
               if(param1.percent)
               {
                  _loc4_ = _loc4_ / this._targetHeight * this._target._height;
               }
               _loc5_ = this._owner.y;
               this._owner.y = _loc3_ + this._target._height + _loc4_;
               this._owner.height = this._owner._rawHeight - (this._owner.y - _loc5_);
               break;
            case ERelationType.BottomExt_Top:
               break;
            case ERelationType.BottomExt_Bottom:
               if(this._owner.editMode != 3 && this._owner == this._target.parent)
               {
                  _loc4_ = this._owner.sourceHeight - (_loc3_ + this._target._initHeight);
               }
               else
               {
                  _loc4_ = this._owner._rawHeight - (_loc3_ + this._targetHeight);
               }
               if(this._owner != this._target.parent)
               {
                  _loc4_ = _loc4_ + this._owner.y;
               }
               if(param1.percent)
               {
                  _loc4_ = _loc4_ / this._targetHeight * this._target._height;
               }
               if(this._owner != this._target.parent)
               {
                  this._owner.height = _loc3_ + this._target._height + _loc4_ - this._owner.y;
               }
               else
               {
                  this._owner.height = _loc3_ + this._target._height + _loc4_;
               }
         }
      }
      
      private function applySelfSizeChanged2(param1:ERelationDef, param2:Number, param3:Number) : void
      {
         switch(param1.type)
         {
            case 3:
            case 5:
               this._owner.x = this._owner.x - param2 / 2;
               break;
            case 4:
            case 6:
               this._owner.x = this._owner.x - param2;
               break;
            case 10:
            case 12:
               this._owner.y = this._owner.y - param3 / 2;
               break;
            case 11:
            case 13:
               this._owner.y = this._owner.y - param3;
         }
      }
      
      private function addRefTarget(param1:EGObject) : void
      {
         if(param1 != this._owner.parent)
         {
            param1.statusDispatcher.addListener(EGObject.XY_CHANGED,this.__targetXYChanged);
         }
         param1.statusDispatcher.addListener(EGObject.SIZE_CHANGED,this.__targetSizeChanged);
         this._targetX = this._target.x;
         this._targetY = this._target.y;
         this._targetWidth = this._target._width;
         this._targetHeight = this._target._height;
      }
      
      private function releaseRefTarget(param1:EGObject) : void
      {
         param1.statusDispatcher.removeListener(EGObject.XY_CHANGED,this.__targetXYChanged);
         param1.statusDispatcher.removeListener(EGObject.SIZE_CHANGED,this.__targetSizeChanged);
      }
      
      private function __targetXYChanged(param1:EGObject) : void
      {
         var _loc6_:ETransition = null;
         if(this._owner.relations.handling || EGObject.snapshotHandling && this._owner.editMode == 2 || this._owner.groupInst != null && this._owner.groupInst.updating != 0 || this._owner is EGGroup && !EGGroup(this._owner).advanced)
         {
            this._targetX = this._target.x;
            this._targetY = this._target.y;
            return;
         }
         this._owner.relations.handling = param1;
         var _loc2_:Number = this._owner.x;
         var _loc3_:Number = this._owner.y;
         var _loc4_:Number = this._target.x - this._targetX;
         var _loc5_:Number = this._target.y - this._targetY;
         this.applyXYChanged(_loc4_,_loc5_);
         this._targetX = this._target.x;
         this._targetY = this._target.y;
         if(_loc2_ != this._owner.x || _loc3_ != this._owner.y)
         {
            _loc2_ = this._owner.x - _loc2_;
            _loc3_ = this._owner.y - _loc3_;
            this._owner.updateGearFromRelations(1,_loc2_,_loc3_);
            if(this._owner.editMode == 1 && this._owner.parent != null && !this._owner.parent.transitions.isEmpty)
            {
               for each(_loc6_ in this._owner.parent.transitions.items)
               {
                  _loc6_.updateFromRelations(this._owner.id,_loc2_,_loc3_);
               }
            }
         }
         this._owner.relations.handling = null;
      }
      
      private function __targetSizeChanged(param1:EGObject) : void
      {
         var _loc6_:ETransition = null;
         if(this._owner.relations.handling || EGObject.snapshotHandling && this._owner.editMode == 2 || this._owner.groupInst != null && this._owner.groupInst.updating != 0 || this._owner is EGGroup && !EGGroup(this._owner).advanced)
         {
            this._targetWidth = this._target._width;
            this._targetHeight = this._target._height;
            return;
         }
         this._owner.relations.handling = param1;
         var _loc2_:Number = this._owner.x;
         var _loc3_:Number = this._owner.y;
         var _loc4_:Number = this._owner._rawWidth;
         var _loc5_:Number = this._owner._rawHeight;
         this.applySizeChanged();
         this._targetWidth = param1._width;
         this._targetHeight = param1._height;
         if(_loc2_ != this._owner.x || _loc3_ != this._owner.y)
         {
            _loc2_ = this._owner.x - _loc2_;
            _loc3_ = this._owner.y - _loc3_;
            this._owner.updateGearFromRelations(1,_loc2_,_loc3_);
            if(this._owner.editMode == 1 && this._owner.parent != null && !this._owner.parent.transitions.isEmpty)
            {
               for each(_loc6_ in this._owner.parent.transitions.items)
               {
                  _loc6_.updateFromRelations(this._owner.id,_loc2_,_loc3_);
               }
            }
         }
         if(_loc4_ != this._owner._rawWidth || _loc5_ != this._owner._rawHeight)
         {
            _loc2_ = this._owner._rawWidth - _loc4_;
            _loc3_ = this._owner._rawHeight - _loc5_;
            this._owner.updateGearFromRelations(2,_loc2_,_loc3_);
         }
         this._owner.relations.handling = null;
      }
   }
}
